import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入多个模块的规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'
Vue.use(VueRouter)

/* Layout */
import Layout from '@/layout'
/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
// 静态路由（所有的用户都拥有的）
export const constantRoutes = [{
    path: '/login',
    // 路由懒加载
    component: () => import('@/views/login/index'),
    hidden: true // 额外增加一个选项hidden:true,目的是控制是否在左侧菜单栏显示
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true // 额外增加一个选项hidden:true,目的是控制是否在左侧菜单栏显示
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard/index',
    children: [{
      path: '/import',
      hidden: true,
      component: () => import('@/views/import'),
      meta: {
        title: '导入',
        icon: 'import'
      }
    }]
  },
  // 改造dashboard路由，让其能够显示在左侧菜单栏
  {
    path: '/dashboard', // 路径
    name: 'Dashboard',
    component: Layout,
    children: [{
      path: 'index',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: {
        title: '首页',
        icon: 'dashboard'
      }
    }]
  }

  // 404 page must be placed at the end !!!
  /* {
    path: '*',
    redirect: '/404',
    hidden: true
  } */
]
// 动态路由
export const asyncRoutes = [
  departmentsRouter,
  approvalsRouter,
  employeesRouter,
  permissionRouter,
  attendancesRouter,
  salarysRouter,
  settingRouter,
  socialRouter
]

const createRouter = () => new VueRouter({
  mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }), // 切换路由时让滚动条回到顶部
  // routes: [...constantRoutes, ...asyncRoutes] // 临时合并动态路由和静态路由
  routes: [...constantRoutes] // 通过路由对象中的addRoutes(新的路由对象)
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
